define(["./bslWebviewObject"],function(webviewObject){
    var UI = {
         description:"系统原生控件",
         launchWebView:null,//加载应用的入口页面
         allViews:null,//暂存所有视图的数组

         initialize:function(){
            var me = this;
            me.launchWebView = me.getLaunchWebView();//获取launch页的信息，异步查询
            var storage = window.localStorage;
            var views = JSON.parse(storage.getItem("allWebViews"));
            //新建存储空间
            if (!views) {
                storage.setItem('allWebViews',JSON.stringify([]));
            }
         },

         //获取localstorage里的数组
         all:function(){
            var storage = window.localStorage;
            var views = JSON.parse(storage.getItem("allWebViews"));
            return views;
         },

         //创建视图，要显示要另外调用show方法
         create:function(url,id,styles){
            var storage = window.localStorage;
            var views = JSON.parse(storage.getItem("allWebViews"));
            //新建存储空间
            if (!views) {
                storage.setItem("allWebViews",JSON.stringify([]));
            }

            var me = this;
            var object = webviewObject.init(url,id,styles);
       
            //将变量临时存储起来,在cordova.exec回调中调用
            var storage = window.localStorage;
            storage.setItem('tempObj',JSON.stringify(object));

            cordova.exec(function(){
                var storage = window.localStorage;
                var obj = JSON.parse(storage.getItem('tempObj'));
                var views = JSON.parse(storage.getItem("allWebViews"));
                views.push(obj);//将变量压入数组
                //更新localstorage的数组
                storage.setItem('allWebViews',JSON.stringify(views));
                         alert(storage.getItem('allWebViews'));
                storage.removeItem('tempObj');

                console.log("create: create successfully");

            },function(){
                //创建视图失败，清楚之前临时存下的变量。
                var storage = window.localStorage;
                storage.removeItem('tempObj');
                consolg.log("create fail");

            },"WebView","createWebView",[url,id,styles]);

            return object;
         },

         //获取launch页信息
         getLaunchWebView:function(){
            // console.log('getLaunchWebView:' + window.location.href);
            var me = this;

            if (!me.launchWebView) {
                me.launchWebView = webviewObject.init(null,"LaunchWebViewID",null);
            }

            cordova.exec(function(dic){
              //更新launch页的属性
                var url = dic["url"];
                var title = dic["title"];
                me.launchWebView.url = url;
                me.launchWebView.viewTitle = title;

              },function(){

                console.log("getLaunchWebview error");

              },"WebView","getLaunchWebview",[]);

            return me.launchWebView;//建议延时使用，因为属性是在回调中更新的
         },

         //删除视图
         close:function(id_wvobj,aniClose,duration){
            var me = this;

            var id;
            if (id_wvobj.viewID) {
                id = id_wvobj.viewID;
            }

            else{
                id = id_wvobj;
            }

            var views = me.all();
            var i;

            for (i=0; i<views.length; i++){
               var obj = views[i];
               if (obj.viewID == id) {
       
                   console.log("close:found");
                   cordova.exec(function(){

                      views.splice(i,1);
                      var storage = window.localStorage;
                      storage.setItem('allWebViews',JSON.stringify(views));
                      var Id = id;
                      window.Listener[Id] = null;//清除监听的事件
                      // console.log( " close end: "+ JSON.stringify(views));

                   },function(){
                       console.log("close fail");
                   },"WebView","closeWebView",[id,aniClose,duration]);

                   break;
               }
            }
         },

         //由原生调用，滑动删除后更新js视图对象数组
         removeView:function(Id){
            var me = this;
            var views = me.all();

            for (var i=0; i<views.length; i++){
                var obj = views[i];

                if (obj.viewID == Id) {
                    views.splice(i,1);
                    var storage = window.localStorage;
                    storage.setItem('allWebViews',JSON.stringify(views));
                    obj.triggerEvent = webviewObject.triggerEvent;

                    obj.triggerEvent("close");
                    break;
                }
            }
         },

         //根据ID获取视图 
         getWebviewById:function(id){
            var me = this;
            var result = null;
            var views = me.all();
            me.allViews = views;//暂时保存

            for (var i=0; i<views.length; i++){
                var obj = views[i];
                if (obj.viewID == id) {
                    result = obj;
                    webviewObject.setupFunctions(result);
                    break;
                }
            } 

            return result;
         },

         //创建并打开视图
         open:function(url, id, styles, aniShow, duration, showedCB){
            var me = this;
            var storage = window.localStorage;
            var views = JSON.parse(storage.getItem("allWebViews"));
            //新建存储空间
            if (!views) {
                storage.setItem('allWebViews',JSON.stringify([]));
            }
            var object = webviewObject.init(url,id,styles);

            cordova.exec(function(){
                         
                var storage = window.localStorage;
                var obj = JSON.parse(storage.getItem('tempObj'));

                var views = JSON.parse(storage.getItem("allWebViews"));
                views.push(obj);
                storage.setItem('allWebViews',JSON.stringify(views));
                storage.removeItem('tempObj');
                console.log( "open success: "+ JSON.stringify(views));
                showedCB();

              },function(error){
                  console.log(error);
                  var storage = window.localStorage;
                  storage.removeItem('tempObj');

              },"WebView","openWebView",[url,id,styles,aniShow,duration]);

            var storage = window.localStorage;
            storage.setItem('tempObj',JSON.stringify(object));

            return object;
         },
          
         //隐藏视图
         hide:function(id_wvobj, aniHide, duration){
            var id;
            if (id_wvobj.viewID){
                id = id_wvobj.viewID;
            }

            else{
                id = id_wvobj;
            }

            cordova.exec(function(){
                console.log("hide suc!");
            },function(){
                console.log("hide error");
            },"WebView","hideWebView",[id,aniHide,duration]);
        },

        //打开视图
        show:function(id_wvobj, aniShow, duration, showedCB){
            var me = this;
            var obj;
            if (id_wvobj.viewID) {
                obj = id_wvobj;
                obj.show = webviewObject.show;
            }

            else{
                obj = me.getWebviewById(id_wvobj);
            }

            obj.show(aniShow,duration,showedCB);
        },

        //获取当前打开着的视图
        currentWebview:function(){
            var me = this;
            var result = null;
            var windowUrl = window.location.href;
            var views = me.all();
       
            for (var i=0;i < views.length; i++){
                var view = views[i];
                if (view.url == windowUrl){
                    result = view;
                    break;
                }
            }

            return result;
        },

        //更新视图的URL和触发监听事件
        updateUrls:function(viewID,url,type){
            var me = this;
            var view = me.getWebviewById(viewID);
            if (view) {
                view.url = url;
               //保存更改
                window.localStorage.setItem('allWebViews',JSON.stringify(me.allViews));
                console.log("updateUrls:trgger begin");
                view.triggerEvent(type);
                return;
            }
        },
        
        //触发监听事件
        triggerEventOfType:function(viewID,type){
            var me = this;
            var obj = me.getWebviewById(viewID);
            if (obj == null) {
                 console.log("triggerEvent obj no found!");
            }

            else{
                obj.triggerEvent(type);
            }
        },
       
       saveViewToLocalStorage:function(obj){
            var me = this;
            var views = me.all();
            var index = 0;
            var isFound = false;
            for(;index < views.length; index++){
                var view = views[index];
                if (view.viewID == obj.viewID){
                    isFound = true;
                    break;
                }
            }
       
            if (isFound){
                views.splice(index,1,obj);
            }
       
            else if (obj && obj != "undefined"){
                views.push(obj);
            }
       
            window.localStorage.setItem('allWebViews',JSON.stringify(views));
       },
    };

	return UI;

});
